
export type GroupName = string;

export interface Groups {
  [key: string]: string[];
}

export interface Config {
  total_participants: number;
  groups: string[];
  total_drawn: number;
  remaining: number;
  redis_connected?: boolean;
}

export interface DrawRequest {
  name: string;
}

export interface DrawResponse {
  success: boolean;
  group?: GroupName;
  name?: string;
  groups?: Groups;
  error?: string;
}

// 比赛相关类型定义

// 轮次现在是动态的字符串，不再硬编码
export type TournamentRound = string;

export type TeamStatus =
  | 'registered'        // 已报名
  | 'grouped'           // 已分组
  | 'advanced'          // 晋级
  | 'eliminated'        // 淘汰
  | 'revived'           // 复活
  | 'final_paired'      // 决赛已配对
  | 'champion'          // 冠军
  | 'runner_up';        // 亚军

export interface Team {
  id: string;
  name: string;
  captain: string;
  group?: string;
  status: TeamStatus;
  round_eliminated?: TournamentRound;
  round_advanced?: TournamentRound;
}

export interface Match {
  id: string;
  round: TournamentRound;
  team1: Team;
  team2: Team;
  winner?: Team;
  loser?: Team;
  completed: boolean;
  match_time?: string;
}

export interface RoundResult {
  advanced: Team[];
  eliminated: Team[];
  groups: { [key: string]: Team[] };
  completed: boolean;
}

export interface TournamentState {
  current_round: TournamentRound;
  draw_enabled: boolean;
  draw_stage?: TournamentRound;
  teams: Team[];
  matches: Match[];
  round_results: { [key: string]: RoundResult };
  champion?: Team;
  runner_up?: Team;
  drawn_captains: string[];

  // 对战分组信息
  round1_groups?: { [key: string]: Team[] };
  revival_groups?: { [key: string]: Team[] };

  // 兼容旧版本的字段（可选）
  round1_advanced?: Team[];
  round1_eliminated?: Team[];
  revival_teams?: Team[];
  round2_teams?: Team[];
  round3_teams?: Team[];
  final_teams?: Team[];
  groups?: { [key: string]: Team[] }; // 旧的初始分组字段
}

// 轮次配置接口
export interface RoundConfig {
  name: string;
  display_name: string;
  groups_count: number;
  teams_per_group: number;
  advance_count: number;
  type: 'main' | 'revival' | 'final';
  order?: number; // 轮次顺序
}

export interface TournamentConfig {
  total_teams: number;
  groups: string[];
  rounds: RoundConfig[];
}

// API 请求和响应类型

export interface StartTournamentRequest {
  teams: { name: string; captain: string }[];
}

export interface StartTournamentResponse {
  success: boolean;
  tournament_state?: TournamentState;
  error?: string;
}



export interface SubmitMatchResultRequest {
  match_id: string;
  winner_team_id: string;
}

export interface RoundStatus {
  completed: boolean;
  can_advance: boolean;
  next_round: string | null;
  current_round: string;
}

export interface SubmitMatchResultResponse {
  success: boolean;
  match?: Match;
  round_status?: RoundStatus;
  tournament_state?: TournamentState;
  message?: string;
  error?: string;
}

export interface AdvanceRoundRequest {
  round: TournamentRound;
}

export interface AdvanceRoundResponse {
  success: boolean;
  tournament_state?: TournamentState;
  error?: string;
}

export interface AvailableRound {
  name: string;
  display_name: string;
  type: string;
  can_switch: boolean;
  reason: string;
}

export interface AvailableNextRoundsResponse {
  success: boolean;
  available_rounds?: AvailableRound[];
  message?: string;
  error?: string;
}

export interface TournamentStatusResponse {
  success: boolean;
  tournament_state?: TournamentState;
  config?: TournamentConfig;
  error?: string;
}

// 轮次管理API类型
export interface AddRoundRequest {
  round: RoundConfig;
}

export interface AddRoundResponse {
  success: boolean;
  message?: string;
  config?: TournamentConfig;
  error?: string;
}

export interface UpdateRoundRequest {
  round: RoundConfig;
}

export interface UpdateRoundResponse {
  success: boolean;
  message?: string;
  config?: TournamentConfig;
  error?: string;
}

export interface DeleteRoundResponse {
  success: boolean;
  message?: string;
  config?: TournamentConfig;
  error?: string;
}

export interface GetConfigResponse {
  success: boolean;
  config?: TournamentConfig;
  error?: string;
}
